# -*- coding: utf-8 -*-

import scrapy
import re
from wallpaper.items import WallpaperItem


class WallpaperSpider(scrapy.Spider):
    name = "wallpaper"
    allowed_domains = ['desk.zol.com.cn']

    def start_requests(self):
        url = "http://desk.zol.com.cn/meinv/gaoqingkuanpingmeinv/"
        yield scrapy.Request(url=url, callback=self.parse_images_group_list)

    def parse_images_group_list(self, response):
        images_urls = response.xpath(
            '//ul//li[@class="photo-list-padding"]/a')
        if not len(images_urls):
            return
        for item_url in images_urls:
            image_url = item_url.xpath('.//@href').extract()[0]
            if not image_url:
                continue
            image_url = "http://desk.zol.com.cn" + str(image_url)
            yield scrapy.Request(url=image_url, callback=lambda response: self.parse_current_group(response))

        # 下一页list
        next_list_url = response.xpath('//a[@id="pageNext"]/@href')
        if len(next_list_url):
            next_list_url = next_list_url.extract()[0]
            next_url = 'http://desk.zol.com.cn' + str(next_list_url)
            print('next_url:', next_url)
            yield scrapy.Request(url=next_url, callback=lambda response: self.parse_images_group_list(response))

    def parse_current_group(self, response):
        exit_image = response.xpath('//img[@id="bigImg"]')
        if not len(exit_image):
            return
        topic = response.xpath('//a[@id="titleName"]')
        items = WallpaperItem()
        items['title'] = topic.xpath('.//text()').extract()[0]
        items['link'] = 'http://desk.zol.com.cn' + topic.xpath('.//@href').extract()[0]
        author = response.xpath(
            '//span[@class="tip"]/a/text()').extract()[0]
        items['author'] = str(author)
        total_num = response.xpath(
            '//div[@class="wrapper photo-tit clearfix"]//h3/span/text()[2]').extract()[0]
        items['total_num'] = re.sub(r'[^\d]+', '', str(total_num))
        items['likes'] = str(response.xpath(
            '//li//a[@class="favorite"]/text()').extract()[0])
        items['unlike'] = str(response.xpath(
            '//li//a[@class="nolike"]/text()').extract()[0])
        image_src = exit_image.xpath('.//@src').extract()[0]
        items['image_path'] = str(image_src).replace('960x600', '1920x1080', 1)
        yield items

        # 下一张
        next_picture = response.xpath('//a[@id="pageNext"]/@href').extract()[0]
        if next_picture:
            next_url = 'http://desk.zol.com.cn' + str(next_picture)
            yield scrapy.Request(url=next_url, callback=lambda response: self.parse_current_group(response))
